package com.bytedy.sboot.support.auth;

import com.bytedy.sboot.support.dto.AuthUserDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Date: 2024/5/28 16:08
 * Author: ByteDy
 */
@Slf4j
@Service
public class UserServiceImpl implements UserDetailsService {

    Map<String, String> STATIC_USERS = Map.of(
            "admin", "{noop}123456",
            "guest", "{noop}123"
    );

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.info("loadUserByUsername:{}", username);
        if (STATIC_USERS.containsKey(username)) {
            // User user = new User(username, STATIC_USERS.get(username), List.of(new SimpleGrantedAuthority("RA_admin")));
            var user = new AuthUserDTO();
            user.setUsername(username);
            user.setPassword(STATIC_USERS.get(username));
            user.setAuthorities(List.of(
                    new SimpleGrantedAuthority("RIGHT_" + "admin"),
                    new SimpleGrantedAuthority("ROLE_" + "admin")
            ));
            log.info("user:{}", user);
            return user;
        }
        throw new UsernameNotFoundException("user not found");
    }
}
